package com.zhny.aiot.jdrk.ec.plugin;

import com.zhny.aiot.jdrk.ec.plugin.model.ECStoreEntry;
import com.zhny.aiot.plugin.ICenterFacilities;
import com.zhny.aiot.plugin.IDeviceFactory;
import com.zhny.aiot.plugin.modbus.ModemEngine;
import com.zhny.aiot.plugin.modbus.device.ModemConfig;
import com.zhny.aiot.plugin.modbus.device.modbus.ModbusNumDevice;
import com.zhny.aiot.plugin.modbus.handler.model.ModemRequestMessage;
import com.zhny.aiot.plugin.modbus.handler.model.ModemResponseMessage;
import com.zhny.aiot.plugin.model.DeviceEntity;
import com.zhny.aiot.plugin.model.TaskEntity;

public class ECDevice extends ModbusNumDevice<ECStoreEntry> {

    public ECDevice(ModemEngine engine, IDeviceFactory<ModemConfig> factory, ICenterFacilities centerFacilities, DeviceEntity entity) {
        super (engine, factory, centerFacilities,entity);
    }

    @Override
    public ECStoreEntry decode(ModemResponseMessage modemResponseMessage) {
        ECStoreEntry entry = new ECStoreEntry ();
        Integer ec = modemResponseMessage.getData ().readUnsignedShort ();
        Integer temperature = modemResponseMessage.getData ().readUnsignedShort ();
        Integer salinity = modemResponseMessage.getData ().readUnsignedShort ();
        entry.setEc (ec<=2000?ec/10.0:ec/1.0);
        entry.setTemperature (temperature/10.0);
        entry.setSalinity (salinity);
        return entry;
    }

    @Override
    public boolean checkError(ModemResponseMessage modemResponseMessage) {
        return modemResponseMessage.getData ().readableBytes () == 6;
    }

    @Override
    protected ModemRequestMessage getCommand(TaskEntity taskEntity) {
        return getFactory ().getRequestMessage (getConfig ());
    }

    @Override
    protected void disposable() {

    }
}
